{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7e9c336f",
   "metadata": {},
   "source": [
    "# 实践：宿舍人脸识别门禁系统\n",
    ">1. 创建FaceSet\n",
    ">2. 查询FaceSet\n",
    ">3.存入face_token数据\n",
    ">4.获取FaceSet信息数据\n",
    ">5.人脸搜索\n",
    ">6.人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2627785",
   "metadata": {},
   "source": [
    "# 1. 创建人脸库 API\n",
    "* 描述\n",
    "> 1. 创建一个人脸的集合 FaceSet，用于存储人脸标识 face_token。一个 FaceSet 能够存储10000个 face_token。试用API Key可以创建1000个FaceSet\n",
    "> 2. 正式API Key可以创建10000个FaceSet。\n",
    "\n",
    "* 调用URL\n",
    "> https://api-cn.faceplusplus.com/facepp/v3/faceset/create\n",
    "\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "92476a3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "6ecc3449",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_creat = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "payload = {\n",
    "    'api_key':'lF2JmrX78SS6-VERJTIAolLXm8_B6Fv8',\n",
    "    'api_secret':'3tIf7ThN8STzEisCsN277l2-AYXwD_pq'\n",
    "}\n",
    "\n",
    "r = requests.post(url=face_creat,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "3cbc4cb1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "8a28b0d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'e631a9699ed390285df8e245e7e7cff3',\n",
       " 'time_used': 157,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648466276,64db6e30-55d0-4d42-a9c5-8642532ec870',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "5efa9020",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9f39f6ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>可选</td>\n",
       "      <td>display_name</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸集合的名字，最长256个字符，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>可选</td>\n",
       "      <td>outer_id</td>\n",
       "      <td>String</td>\n",
       "      <td>账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>可选</td>\n",
       "      <td>tags</td>\n",
       "      <td>String</td>\n",
       "      <td>FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>可选</td>\n",
       "      <td>face_tokens</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>可选</td>\n",
       "      <td>user_data</td>\n",
       "      <td>String</td>\n",
       "      <td>自定义用户信息，不大于16 KB，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>可选</td>\n",
       "      <td>force_merge</td>\n",
       "      <td>Int</td>\n",
       "      <td>在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  是否必选           参数名      类型  \\\n",
       "0   必选       api_key  String   \n",
       "1   必选    api_secret  String   \n",
       "2   可选  display_name  String   \n",
       "3   可选      outer_id  String   \n",
       "4   可选          tags  String   \n",
       "5   可选   face_tokens  String   \n",
       "6   可选     user_data  String   \n",
       "7   可选   force_merge     Int   \n",
       "\n",
       "                                                参数说明  \n",
       "0                                  调用此 API 的 API Key  \n",
       "1                               调用此 API 的 API Secret  \n",
       "2                    人脸集合的名字，最长256个字符，不能包括字符^@,&=*'\"  \n",
       "3  账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...  \n",
       "4  FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...  \n",
       "5  人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...  \n",
       "6                    自定义用户信息，不大于16 KB，不能包括字符^@,&=*'\"  \n",
       "7  在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<div class=\"table-wrap\"><table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Secret</span></p></td></tr><tr><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">display_name</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">人脸集合的名字，最长<span class=\"inline-comment-marker\" data-ref=\"9f098728-ac30-41a2-aab6-05b79f8a7d2e\">256</span>个字符，不能包括字符^@,&=*'\"</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255个字符，<span class=\"inline-comment-marker\" data-ref=\"63fc0fd1-cd44-4256-bab5-971a939d66e2\"><span class=\"inline-comment-marker\" data-ref=\"183dcb11-c22c-47f4-aa9c-2ba6f3c8210c\">不能包括字符^@,&=*'\"</span></span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长<span class=\"inline-comment-marker\" data-ref=\"1be760b6-f20d-4ff3-a086-b7f763abd84d\">255</span>个字符，多个 tag 用逗号分隔，每个 tag 不能包括字符^@,&=*'\"</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">可选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">face_tokens</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_token</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">user_data</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">自定义用户信息，不大于<span class=\"inline-comment-marker\" data-ref=\"f9edadb1-691d-4d80-933e-909096780f61\">16 KB，不能包括字符^@,&=*'\"</span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">force_merge</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_token 加入已经存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">0：不将 face_tokens 加入已存在的 FaceSet 中，直接返回 FACESET_EXIST 错误</span></p><p><span style=\"color: rgb(0,0,0);\">1：将 face_tokens 加入已存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">默认值为0</span></p></td></tr></tbody></table></div>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "09afbccb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A-0 准备工作\n",
    "import requests\n",
    "API_Key = 'lF2JmrX78SS6-VERJTIAolLXm8_B6Fv8'\n",
    "API_Secret = '3tIf7ThN8STzEisCsN277l2-AYXwD_pq'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f91456d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A-0 创建人脸数据集合\n",
    "FaceSet_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_Secret\n",
    "}\n",
    "r = requests.post(url=FaceSet_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "008093df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8b30651f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'b27045b25b7049c8ea9a035a34809eed',\n",
       " 'time_used': 152,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648465029,98b86dc1-4d6b-4587-91f6-8b082cc4c410',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a325d81",
   "metadata": {},
   "source": [
    "## 2.查询FaceSet\n",
    "\n",
    "* 描述\n",
    "> 1. 获取一个 FaceSet 的所有信息，包括此 FaceSet 的 faceset_token, outer_id, display_name 的信息，以及此 FaceSet 中存放的 face_token 数量与列表。\n",
    "> 2. 注意：2017年8月16日后，调用本接口将不会一次性返回全部的 face_token 列表。单次查询最多返回 100 个 face_token。如需获取全量数据，需要配合使用 start 和 next 参数。请尽快修改调整您的程序。\n",
    "\n",
    "* 调用URL\n",
    "> https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\n",
    "\n",
    "* 调用方法\n",
    "> POST\n",
    "\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "69f37328",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此API的API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>必选(二选一)</td>\n",
       "      <td>faceset_token</td>\n",
       "      <td>String</td>\n",
       "      <td>FaceSet的标识</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>必选(二选一)</td>\n",
       "      <td>outer_id</td>\n",
       "      <td>String</td>\n",
       "      <td>用户提供的FaceSet标识</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>可选（2017年8月16日起可使用）</td>\n",
       "      <td>start</td>\n",
       "      <td>Int</td>\n",
       "      <td>一个数字 n，表示开始返回的 face_token 在本 FaceSet 中的序号， n 是...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 是否必选            参数名      类型  \\\n",
       "0                  必选        api_key  String   \n",
       "1                  必选     api_secret  String   \n",
       "2             必选(二选一)  faceset_token  String   \n",
       "3             必选(二选一)       outer_id  String   \n",
       "4  可选（2017年8月16日起可使用）          start     Int   \n",
       "\n",
       "                                                参数说明  \n",
       "0                                     调用此API的API Key  \n",
       "1                                  调用此API的API Secret  \n",
       "2                                         FaceSet的标识  \n",
       "3                                     用户提供的FaceSet标识  \n",
       "4  一个数字 n，表示开始返回的 face_token 在本 FaceSet 中的序号， n 是...  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<div class=\"table-wrap\"><table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此API的API Secret</span></p></td></tr><tr><td rowspan=\"2\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">必选(二选一)</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">faceset_token</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">FaceSet的标识</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">用户提供的FaceSet标识</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">可选</span></p><p><span style=\"color: rgb(0,0,0);\">（2017年8月16日起可使用）</span></p></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">start</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">一个数字 n，表示开始返回的 face_token 在本 FaceSet 中的序号， n 是 [1,10000] 间的一个整数。 </span></p><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">通过传入数字 n，可以控制本 API 从第 n 个 <span class=\"s1\">face_token </span>开始返回。返回的 face_token 按照创建时间排序，每次返回 <span class=\"s1\">100 </span>个 <span class=\"s1\">face_token</span>。</span></p><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">默认值为 1。</span></p><p class=\"p1\"><span style=\"color: rgb(0,0,0);\">您可以输入上一次请求本 API 返回的 next 值，用以获得接下来的 100 个 face_token。</span></p></td></tr></tbody></table></div>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "dc2e8c12",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 书写B-1 检查自己的数据集合是否创建成功及是否更新成功\n",
    "# B-1\n",
    "Getdetail_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_Secret,\n",
    "    'faceset_token':'0d221172aa11bd995f966f5e75dea4ea'\n",
    "}\n",
    "r = requests.post(url=Getdetail_url,params=payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "b92feaee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "1c63f52e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '0d221172aa11bd995f966f5e75dea4ea',\n",
       " 'tags': '',\n",
       " 'time_used': 78,\n",
       " 'user_data': '59人',\n",
       " 'display_name': '2020网新API周二上午班',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1648466652,0f0798e7-da50-433e-a547-d95bec7f7d80',\n",
       " 'outer_id': 'API020305'}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f52d20ae",
   "metadata": {},
   "source": [
    "## 3.更新人脸库信息 API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "aba434f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# C-1\n",
    "update_faceset_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_Secret,\n",
    "    'faceset_token':'0d221172aa11bd995f966f5e75dea4ea',\n",
    "    'new_outer_id':'API020305',  # 自定义表符号\n",
    "    'display_name':'2020网新API周二上午班',\n",
    "    'user_data':'59人'\n",
    "}\n",
    "r = requests.post(url=update_faceset_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "93483bff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "549fbe88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 71,\n",
       " 'error_message': 'NEW_OUTER_ID_EXIST',\n",
       " 'request_id': '1648465030,3bb4c256-2055-475d-9834-bd6c1bb36342'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc619331",
   "metadata": {},
   "source": [
    "* 功能型API\n",
    "> 人脸识别\n",
    "> 人脸检测 API\n",
    "> 人脸分析 API\n",
    "> 人脸比对 API\n",
    "> 人脸搜索 API\n",
    "* 数据库操作\n",
    "> 创建人脸库 API\n",
    "> 删除人脸库 API\n",
    "> 更新人脸库信息 API\n",
    "> 获取人脸库列表及信息 API\n",
    "> 获取人脸库信息 API\n",
    "> 添加人脸 API\n",
    "> 删除人脸 API\n",
    "> 添加人脸（异步）API\n",
    "> 删除人脸（异步）API\n",
    "> 添加/删除人脸结果查询 API\n",
    "> 人脸库中的人脸管理 API 组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0ef05918",
   "metadata": {},
   "outputs": [],
   "source": [
    "getfacesets_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_Secret\n",
    "}\n",
    "r = requests.post(url=getfacesets_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9b6ee29d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "14fe973c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 104,\n",
       " 'facesets': [{'faceset_token': '5e8bfc5de3bf3c178b81ade2dfa716b4',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'c2f65c39431319db310ffeeceb681491',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '4deb65d367807e027e48e318b17572c7',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '0d221172aa11bd995f966f5e75dea4ea',\n",
       "   'outer_id': 'API020305',\n",
       "   'display_name': '2020网新API周二上午班',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'e820401d7c16f8716c64d55a6a1994e6',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '75af658966f5e141854379243823064e',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '3c5d0c7eb4114d1cb032f02f2eca3205',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'b27045b25b7049c8ea9a035a34809eed',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648465030,c3b162fa-a507-4097-920d-d2c7d2e83d0f'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d436082f",
   "metadata": {},
   "source": [
    "#  2.实现所有人脸功能的应用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "104e7b5a",
   "metadata": {},
   "source": [
    "### 1.实现人脸检测获取face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3477e816",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "bf4bd682",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_Key, API_Sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\asus\\api\\week04\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a5850d79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648465497,ebdc0770-5527-4a32-9cf7-dce9675efcfd',\n",
       " 'time_used': 170,\n",
       " 'faces': [{'face_token': 'd9ec5ad52bfda27dfd539bb32aecc959',\n",
       "   'face_rectangle': {'top': 547, 'left': 279, 'width': 369, 'height': 369}}],\n",
       " 'image_id': 'wAe8xB+XXTJ9R2AVKv41Mg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_Secret,'1.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "b8e16536",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648465637,3d5ec1b5-391d-487a-bbca-30612c32b9d6',\n",
       " 'time_used': 213,\n",
       " 'faces': [{'face_token': '2feb0b6727f6f899ceb4373c6f3c62d3',\n",
       "   'face_rectangle': {'top': 870, 'left': 357, 'width': 447, 'height': 447}}],\n",
       " 'image_id': 'WlP9Nb57HhFVFU2QaJq6JA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_Secret,'2.jpg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e3f8eb7",
   "metadata": {},
   "source": [
    "### 3.本周作业\n",
    "\n",
    "* 1. 尝试创建detect.py，并实现功能调用\n",
    "* 2. 尝试创建analyze.py/compare.py/search.py,并实现功能调用\n",
    "* 3. 尝试存放宿舍4位同学的人脸信息到人脸数据集（xxx栋xxx宿舍）\n",
    "> 1. 完善faceset信息\n",
    "> 2. 实现人脸搜索和人脸对比，打印结果：是本人or不是本人"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52d0d556",
   "metadata": {},
   "source": [
    "### 2.实现人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "896a4c1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "1fe7b090",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module analyze:\n",
      "\n",
      "NAME\n",
      "    analyze\n",
      "\n",
      "FUNCTIONS\n",
      "    face_analyze(API_key, API_sercret, face_token, attributes)\n",
      "        该函数调用face++ face_analyze接口,用于分析人脸\n",
      "\n",
      "FILE\n",
      "    c:\\users\\asus\\api\\week04\\analyze.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "09617975",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 189,\n",
       " 'request_id': '1648465624,21843542-1227-4800-8240-2137758a3fd1',\n",
       " 'faces': [{'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 24},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.009}},\n",
       "   'face_rectangle': {'width': 369, 'top': 547, 'left': 279, 'height': 369},\n",
       "   'face_token': 'd9ec5ad52bfda27dfd539bb32aecc959'}]}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_Key,API_Secret,'d9ec5ad52bfda27dfd539bb32aecc959','gender,age,smiling')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a1c35fe",
   "metadata": {},
   "source": [
    "## 3.实现人脸比对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "c317e8d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "451fc2d0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module compare:\n",
      "\n",
      "NAME\n",
      "    compare\n",
      "\n",
      "FUNCTIONS\n",
      "    face_compare(API_key, API_sercret, face_token1, face_token2)\n",
      "        该函数调用face++ face_compare接口,用于人脸对比\n",
      "\n",
      "FILE\n",
      "    c:\\users\\asus\\api\\week04\\compare.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(compare)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "131398b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648465733,8cfc6cc6-1508-4b45-a721-ee1aa7e22051',\n",
       " 'time_used': 408,\n",
       " 'confidence': 60.06,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare.face_compare(API_Key,API_Secret,'d9ec5ad52bfda27dfd539bb32aecc959','2feb0b6727f6f899ceb4373c6f3c62d3')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da6b46ef",
   "metadata": {},
   "source": [
    "## 4.实现人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "50198872",
   "metadata": {},
   "outputs": [],
   "source": [
    "import search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "8db37455",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module search:\n",
      "\n",
      "NAME\n",
      "    search\n",
      "\n",
      "FUNCTIONS\n",
      "    face_search(API_key, API_sercret, face_token, faceset_token)\n",
      "        该函数调用face++ face_search接口,用于人脸搜索\n",
      "\n",
      "FILE\n",
      "    c:\\users\\asus\\api\\week04\\search.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8e6ded0",
   "metadata": {},
   "outputs": [],
   "source": [
    "search.face_search(API_key, API_sercret,'d9ec5ad52bfda27dfd539bb32aecc959', 'e631a9699ed390285df8e245e7e7cff3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b912762a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
